<!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>权限管理</title>
	    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css" />
	    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css" />
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css" />
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css" />
	    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
	    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    </head>
    <body>
    	   <div class="easyui-container-fluid">
             <div class="easyui-panel" style="border:none;">
                 <div id="nav" class="nav"></div>
                 <div class="easyui-panel" style="border:none;">
                     <form id="ff">
                         <div style="margin-bottom:20px;">
                             <label class="item-label">状态：</label>
                             <select class="easyui-combobox" name="entityStatus" style="width:128px;" data-options="panelHeight:'auto'">
                                 <option value="">-请选择-</option>
                                 <option value="OK" selected>启用</option>
                                 <option value="DISABLE">禁用</option>
                             </select>
                         </div>
                         <div style="margin-bottom: 20px;text-align: center">
                             <a class="easyui-linkbutton c8" href="javascript:void(0);" onclick="searchBy()" style="width:80px;">查询</a>
                         </div>
                     </form>
                     <table id="datagrid" class="easyui-datagrid" data-options="fitColumns:true,scrollbarSize:0">
                         <thead>
                             <tr>
                                 <th data-options="field:'parentName',align:'center'" width="80">名称</th>
                                 <th data-options="field:'name',align:'center'" width="80">名称</th>
                                 <th data-options="field:'url',align:'center'" width="150">URL</th>
                                 <th data-options="field:'level',align:'center'" width="100">菜单等级</th>
                                 <th data-options="field:'createDate',align:'center'" width="80">添加时间</th>
                                 <th data-options="field:'action',align:'center',formatter:actions" width="160">操作</th>
                             </tr>
                         </thead>
                         <tbody></tbody>
                     </table>
                 </div>
             </div>
         </div>
         <script type="text/javascript">
             function initPage() {
                 var dtd = $.Deferred();
                 var height = $(window).height();
                 var nav_height = $("#nav")._outerHeight();
                 var ff_height = $("#ff").height();
                 var table_height = height-nav_height-ff_height-20;
                 $("#datagrid").datagrid("resize",{height:table_height});
                 $("#datagrid").datagrid({
                     toolbar:[{
                         text:'添加权限',
                         handler:function(){
                             window.location.href = "/webapp/pages/system/permission/add.html";
                         }
                     }],
                     loadFilter:function (data) {
                         var total = 0,rows = [],merges = [];
                         if(data.code==401){
                             //window.parent.location.href="/webapp"
                             return {total:0,rows:[]};
                         }
                         if(data){
                             total = data.totalElements;
                             rows = data.content;
                             var res = groupBy(rows,"parentId");
                             rows = res.rows;
                             merges = res.merges;
                         }
                         return {total:total,rows:rows,merges:merges};
                     },
                     onLoadSuccess:function(data){
                         var merges = data.merges;
                         for(var i=0;i<merges.length;i++){
                             $("#datagrid").datagrid("mergeCells",{
                                 index:merges[i].index,
                                 rowspan:merges[i].rowspan,
                                 field:"parentName"
                             });
                         }
                     }
                 });
                 dtd.resolve();
                 return dtd.promise();
             }
             function groupBy(data,prop){
                 var result = [];
                 var dataMap = {};
                 var pair = {};
                 data.map(function(item,index){
                    pair[item.id] = item;
                    if(dataMap[item[prop]]){
                        dataMap[item[prop]].push(item);
                    }else{
                        dataMap[item[prop]] = [item];
                    }
                 });
                 //result = dataMap[""];
                 //changeProp(result,dataMap);
                 var count = 0,merges=[];
                 for(var k in dataMap){
                     var arr = dataMap[k];
                     merges.push({index:count,rowspan:arr.length});
                     arr.map(function(item){
                        item.parentName = pair[item.parentId]&&pair[item.parentId].name||"无父级";
                        result.push(item);
                        count++;
                     });
                 }
                 return {rows:result,merges:merges};
             }
             function changeProp(data,groups){
                 if(data){
                     data.map(function(item,index){
                         var children = groups[item.id];
                         item.children = children;
                         changeProp(item.children,groups);
                         return item;
                     });
                 }
                 return data;
             }
         </script>
         <script type="text/javascript" src="/webapp/js/common/pagelist.js"></script>
    </body> 
</html>
